<?php  
/*专业，模型*/
class School_admin_model extends CI_Model
{

    var $table='school_admin';

    function __construct()
    {
        parent::__construct();
        
    }

    /**
     * 返回数据表总条目个数
     * @return [type] [description]
     */
    function count_all()
    {
        return $this->db->count_all($this->table);
    }


    /**
     * 获得所有的条目
     * @return [type] [description]
     */
    function get_all()
    {
        $res=$this->db->get($this->table)->result_array();
        return $res;
    }

    /**
     * 通过id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function get_by_id($id)
    {
        return $this->db->get_where($this->table, array('id' => $id ))->row_array();
    }
    /**
     * 通过id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function get_by_user_id($user_id)
    {
        return $this->db->get_where($this->table, array('user_id' => $user_id ))->row_array();
    }
    /**
     * 通过school_id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function get_by_school_id($school_id)
    {
        return $this->db->get_where($this->table, array('school_id' => $school_id ))->result_array();
    }

    function get_by_school_id_with_user_info($school_id)
    {
        $sql='SELECT u.username as username,sa.name as name,sa.id as id,sa.user_id as user_id,sa.school_id as school_id FROM user as u,school_admin as sa WHERE u.id=sa.user_id and sa.school_id=?';
        return $this->db->query($sql,$school_id)->result_array();
    }

    /**
     * 添加一个条目
     * @param [type] $item [description]
     */
    function add($item)
    {
        return  $this->db->insert($this->table,$item);
    }

    /**
     * 删除一个条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    function delete($id)
    {
        return $this->db->delete($this->table, array('id' => $id ));

    }

    /**
     * 更新一个条目
     * @param  [type] $id   [description]
     * @param  [type] $item [description]
     * @return [type]       [description]
     */
    function update($id,$item)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table,$item);
    }
}
